package com.lanyuan.controller.app;

import com.lanyuan.annotation.SystemLog;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.entity.app.DiagnosisAnswerFormMap;
import com.lanyuan.entity.app.DiagnosisQuestionFormMap;
import com.lanyuan.exception.SystemException;
import com.lanyuan.mapper.app.DiagnosisMapper;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.Common;
import com.lanyuan.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.util.List;

@Controller
@RequestMapping("/app/diagnosis/")
public class DiagnosisController extends BaseController {
    @Inject
    private DiagnosisMapper diagnosisMapper;

    @RequestMapping("questionList")
    public String questionList(Model model) throws Exception {
        model.addAttribute("res", findByRes());
        return Common.BACKGROUND_PATH + "/diagnosis/question/list";
    }

    @ResponseBody
    @RequestMapping("findByPage")
    public PageView findByPage(String pageNow, String pageSize, String column, String sort) throws Exception {
        DiagnosisQuestionFormMap formMap = getFormMap(DiagnosisQuestionFormMap.class);
        formMap = toFormMap(formMap, pageNow, pageSize, formMap.getStr("orderby"));
        formMap.put("column", column);
        formMap.put("sort", sort);
        PageView pageView = getPageView(formMap);
        pageView.setRecords(diagnosisMapper.findQuestionPage(formMap)); // 不调用默认分页,调用自已的mapper中findPage
        return pageView;
    }

    @RequestMapping("answerList")
    public String answerList(Model model) throws Exception {
        String question_id = getPara("question_id");
        model.addAttribute("question_id", question_id);
        if(StringUtils.isNotBlank(question_id)){
            DiagnosisQuestionFormMap question = diagnosisMapper.findOne("id", question_id, DiagnosisQuestionFormMap.class);
            model.addAttribute("selectedIds", question.getStr("reply_answer_ids"));

        }
        return Common.BACKGROUND_PATH + "/diagnosis/question/answerSelect";
    }

    @ResponseBody
    @RequestMapping("findAnswerByPage")
    public PageView findAnswerByPage(String pageNow, String pageSize, String column, String sort) throws Exception {
        DiagnosisAnswerFormMap formMap = getFormMap(DiagnosisAnswerFormMap.class);
        formMap = toFormMap(formMap, pageNow, "1000", formMap.getStr("orderby"));
        formMap.put("column", column);
        String qid = getPara("question_id");
        formMap.put("question_id",getPara("question_id"));
        formMap.put("sort", sort);
        PageView pageView = getPageView(formMap);
        pageView.setRecords(diagnosisMapper.findAnswerPage(formMap)); // 不调用默认分页,调用自已的mapper中findPage

        return pageView;
    }

    @RequestMapping("addUI")
    public String addUI(Model model) throws Exception {
        return Common.BACKGROUND_PATH + "/diagnosis/question/add";
    }

    @ResponseBody
    @RequestMapping("addEntity")
    @SystemLog(module = "配置管理", methods = "新增问诊题目") // 凡需要处理业务逻辑的.都需要记录操作日志
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    public String addEntity() {
        try {
            DiagnosisQuestionFormMap formMap = getFormMap(DiagnosisQuestionFormMap.class);
            formMap.put("status","1");
            formMap.put("create_time", DateUtil.formatDateTime());
            diagnosisMapper.addEntity(formMap); // 新增后返回新增信息
            String answerIds = formMap.getStr("reply_answer_ids");

            if(StringUtils.isNotBlank(answerIds)){
                diagnosisMapper.answerPointQuestion(formMap.getLong("id"),answerIds.split(","));
            }
            String[] answers = getParaValues("answers");
            for (String answer : answers) {
                DiagnosisAnswerFormMap answerFormMap = new DiagnosisAnswerFormMap();
                answerFormMap.put("title",answer);
                answerFormMap.put("question_id",formMap.get("id"));
                answerFormMap.put("status","1");
                answerFormMap.put("create_time", DateUtil.formatDateTime());
                diagnosisMapper.addEntity(answerFormMap);
            }
            System.out.println(answers);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException("添加配置异常");
        }
        return "success";
    }

    @RequestMapping("editUI")
    public String editUI(Model model) {
        String id = getPara("id");
        if (Common.isNotEmpty(id)) {
            DiagnosisQuestionFormMap formMap = diagnosisMapper.findOne("id", id, DiagnosisQuestionFormMap.class);
            List<DiagnosisAnswerFormMap> answers = diagnosisMapper.findAnswerByQuestionId( id);
            String answerIds = formMap.getStr("reply_answer_ids");
            if(StringUtils.isNotBlank(answerIds)) {
                if(answerIds.contains(",")) {
                    List<DiagnosisAnswerFormMap> replyAnswers = diagnosisMapper.findReplyAnswers(answerIds.split(","));
                    String names = "";
                    for (DiagnosisAnswerFormMap replyAnswer : replyAnswers) {
                        names += replyAnswer.getStr("title")+",";
                    }
                    model.addAttribute("reply_answer_names", names.substring(0,names.lastIndexOf(",")));
                }else {
                    DiagnosisAnswerFormMap replyAnswer = diagnosisMapper.findOne("id", answerIds, DiagnosisAnswerFormMap.class);
                    model.addAttribute("reply_answer_names", replyAnswer.getStr("title"));
                }
            }

            model.addAttribute("answers", answers);
            model.addAttribute("diagnosisQuestion", formMap);
        }
        return Common.BACKGROUND_PATH + "/diagnosis/question/edit";
    }

    @ResponseBody
    @RequestMapping("editEntity")
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    @SystemLog(module = "配置管理", methods = "修改问诊题目信息") // 凡需要处理业务逻辑的.都需要记录操作日志
    public String editEntity() throws Exception {
        String code = "success";
        DiagnosisQuestionFormMap formMap = getFormMap(DiagnosisQuestionFormMap.class, false);
        diagnosisMapper.editEntity(formMap);
        //要依赖的 选项id
        String replyAnswerIds = formMap.getStr("reply_answer_ids");
        String qid = formMap.getStr("id");
        if(StringUtils.isNotBlank(replyAnswerIds)){
            //首先将之前选项 point_question_id 为当前题目的 设置为空。
            diagnosisMapper.relieveAnswerPointQuestionId(qid);
            //将依赖选项的 指向题目id point_question_id 设置为当前题目id
            diagnosisMapper.answerPointQuestion(Long.valueOf(qid),replyAnswerIds.split(","));
        }
        //首先将该题目的所有选项 question_id更新为0 不属于任何题目
        DiagnosisAnswerFormMap answerFormMap = new DiagnosisAnswerFormMap();
        answerFormMap.put("question_id","0");
        diagnosisMapper.relieveAnswerByQuestionId(qid);
        String[] answers = getParaValues("answers");
        String[] answerIds = getParaValues("answerIds");
        //循环id数组，id不为0则进行修改操作否则进行新增操作
        for (int i = 0; i < answerIds.length; i++) {
            String answerId = answerIds[i];
             if(!answerId.equals("0")){
                DiagnosisAnswerFormMap answer = diagnosisMapper.findOne("id", answerId, DiagnosisAnswerFormMap.class);
                answer.put("title",answers[i]);
                answer.put("question_id",formMap.get("id"));
                diagnosisMapper.editEntity(answer);
            }else{
                DiagnosisAnswerFormMap answer = new DiagnosisAnswerFormMap();
                answer.put("title",answers[i]);
                answer.put("question_id",formMap.get("id"));
                answer.put("status","1");
                answer.put("create_time", DateUtil.formatDateTime());
                diagnosisMapper.addEntity(answer);
            }
        }
        diagnosisMapper.deleteByAttribute("question_id","0",DiagnosisAnswerFormMap.class);
        return code;
    }

    @ResponseBody
    @RequestMapping("deleteEntity")
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    @SystemLog(module = "配置管理", methods = "问诊管理-删除题目") // 凡需要处理业务逻辑的.都需要记录操作日志
    public String deleteEntity() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            diagnosisMapper.deleteByAttribute("id", id, DiagnosisQuestionFormMap.class);
            diagnosisMapper.deleteByAttribute("question_id", id, DiagnosisAnswerFormMap.class);
        }
        return "success";
    }

    // 更改盟友展业状态
    @RequestMapping("editStatus")
    @ResponseBody
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    @SystemLog(module = "配置管理", methods = "更改问诊题目状态") // 凡需要处理业务逻辑的.都需要记录操作日志
    public String editStatus() throws Exception {
        String id = getPara("id");
        String status = getPara("status");
        DiagnosisQuestionFormMap userFormMap = getFormMap(DiagnosisQuestionFormMap.class);
        userFormMap.set("id", id);
        userFormMap.set("status", status);
        diagnosisMapper.editEntity(userFormMap);
        return "success";
    }



}
